package com.cqc.paoclient.ui.search.code;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.SearchView;

import com.cqc.paoclient.R;
import com.cqc.paoclient.adapter.CodeRvAdapter;
import com.cqc.paoclient.base.BaseFragment;
import com.cqc.paoclient.model.CodeModel;
import com.cqc.paoclient.ui.code.codedetail.CodeDetailActivity;
import com.cqc.paoclient.util.LogUtil;
import com.cqc.paoclient.util.Util;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by cui on 2017/11/26
 */

public class CodeFragment extends BaseFragment<CodeFragPresenter> implements CodeFragContract.View {

    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.searchView)
    SearchView searchView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.refreshLayout)
    SwipyRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private List<CodeModel.ItemsBean> mItems;
    private CodeRvAdapter adapter;
    private String keyword;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.frag_search_code;
    }

    @Override
    protected CodeFragPresenter getmPresenter() {
        return new CodeFragPresenter();
    }

    @Override
    protected void presenterSetView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
    }

    @Override
    protected void initView() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        refreshLayout.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light, android.R.color.holo_orange_light);
        refreshLayout.setOnRefreshListener(onRefreshListener);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
    }

    @Override
    protected void initData() {
        mItems = new ArrayList<>();
        adapter = new CodeRvAdapter(mItems);
        recyclerView.setAdapter(adapter);

        adapter.setmItemClickListener(new CodeRvAdapter.ItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(getContext(),CodeDetailActivity.class);
                intent.putExtra("id", mItems.get(position).getId());
                intent.putExtra("title", mItems.get(position).getTitle());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                onRefreshListener.onRefresh(SwipyRefreshLayoutDirection.TOP);
                refreshLayout.setRefreshing(true);
            }
        }, 1000);
    }

    @Override
    public void getCodeListSuccess(List<CodeModel.ItemsBean> itemsBeen) {
        stopRefresh();
        mItems.clear();
        mItems.addAll(itemsBeen);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getCodeListFail(String error) {
        LogUtil.d(error);
        stopRefresh();
    }

    @Override
    public void loadMoreCodeListSuccess(List<CodeModel.ItemsBean> itemsBeen) {
        stopRefresh();
        mItems.addAll(itemsBeen);
        adapter.notifyDataSetChanged();
    }

    private void stopRefresh() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }

    private SwipyRefreshLayout.OnRefreshListener onRefreshListener = new SwipyRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh(final SwipyRefreshLayoutDirection direction) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (direction == SwipyRefreshLayoutDirection.TOP) {
                        mPresenter.getCodeList(keyword);
                    } else {
                        mPresenter.loadMoreCodeList(keyword);
                    }
                }
            });
        }
    };


    private void search() {
        keyword = et.getText().toString().trim();
        refreshLayout.setRefreshing(true);
        onRefreshListener.onRefresh(SwipyRefreshLayoutDirection.TOP);
        Util.hideInput(et);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.tab_2_frag, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().finish();
                break;
            case R.id.app_bar_search:
                search();
                break;
        }
        return true;
    }


}
